package com.zhuss.black.black;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.black.black.DetailBlackBo;
import com.zhuss.bean.bo.black.black.SearchBlackBo;
import com.zhuss.bean.bo.black.black.wechat.ListBlackBo;
import com.zhuss.bean.bo.core.upload.DownloadBo;
import com.zhuss.bean.vo.black.black.DetailBlackVo;
import com.zhuss.bean.vo.black.black.ListBlackVo;
import com.zhuss.bean.vo.core.upload.DownloadVo;
import com.zhuss.black.processor.BlackAppendRead;
import com.zhuss.black.processor.BlackRead;
import com.zhuss.black.processor.BlackTopicRead;
import com.zhuss.common.PageVo;
import com.zhuss.common.Processor;
import com.zhuss.common.ReadService;
import com.zhuss.enums.AppendTypeEnum;
import com.zhuss.exception.WpsException;


/**
 * black查询service（基本查询）
 * @author zhushanshuo
 * @Date 2020-02-05 15:59
 *
 */
@Component("blackReadService")
public class BlackReadService implements ReadService<Object, Object> {
	
	@Autowired
	@Qualifier("blackRead")
	private BlackRead blackRead;

	@Autowired
	@Qualifier("blackTopicRead")
	private BlackTopicRead blackTopicRead;
	
	@Autowired
	@Qualifier("blackAppendRead")
	private BlackAppendRead blackAppendRead;
	
	@Autowired
	@Qualifier("downloadProcessor")
	private Processor<DownloadVo, DownloadBo> downloadProcessor;
	
	
	
	/**
	 * 详情
	 */
	@Override
	public Object detail(Object param) throws WpsException {
		DetailBlackBo bo = (DetailBlackBo) param;
		Long blackId = bo.getBlackId();
		DetailBlackVo vo = (DetailBlackVo) blackRead.detail(blackId);
		DownloadBo downloadBo = new DownloadBo();
		downloadBo.setKeyList(vo.getPic());
		DownloadVo downloadVo = downloadProcessor.process(downloadBo);
		vo.setUrlList(downloadVo.getUrlList());
		return vo;
	}
	
	/**
	 * 列表
	 * @throws WpsException 
	 */
	@Override
	public Object list(Object param) throws WpsException {
		SearchBlackBo bo = (SearchBlackBo) param;
		Object result = blackRead.list(bo);
		transfer(result, bo.getConcurrentUserId());
		return result;
	}
	
	/**
	 * 树
	 * @throws WpsException 
	 */
	public Object listTopicTree(DetailBlackBo param) throws WpsException {
		Long blackId = param.getBlackId();
		Object result = blackTopicRead.listTopicTree(blackId);
		return result;
	}
	
	/**
	 * 列表
	 * @throws WpsException 
	 */
	public Object listBlack(Object param) throws WpsException {
		ListBlackBo bo = (ListBlackBo) param;
		Object result = blackRead.listBlack(bo);
		transfer2(result, bo.getConcurrentUserId());
		return result;
	}
	
	@SuppressWarnings("unchecked")
	private void transfer2(Object result, Long userId) throws WpsException {
		PageVo<DetailBlackVo> pageVo = (PageVo<DetailBlackVo>) result;
		for (DetailBlackVo vo : pageVo.getItems()) {
			DownloadBo downloadBo = new DownloadBo();
			downloadBo.setKeyList(vo.getPic());
			DownloadVo downloadVo = downloadProcessor.process(downloadBo);
			vo.setUrlList(downloadVo.getUrlList());
			
			Integer thumbNum = blackAppendRead.listByType(AppendTypeEnum.点赞.key(), vo.getBlackId(), null).size();
			Integer collectNum = blackAppendRead.listByType(AppendTypeEnum.收藏.key(), vo.getBlackId(), null).size();
			Integer forwardNum = blackAppendRead.listByType(AppendTypeEnum.转发.key(), vo.getBlackId(), null).size();
			Integer topicNum = blackTopicRead.listTopic(vo.getBlackId(), 0l).size();
			
			Integer thumbSelfNum = blackAppendRead.listByType(AppendTypeEnum.点赞.key(), vo.getBlackId(), userId).size();
			Integer collectSelfNum = blackAppendRead.listByType(AppendTypeEnum.收藏.key(), vo.getBlackId(), userId).size();
			Integer forwardSelfNum = blackAppendRead.listByType(AppendTypeEnum.转发.key(), vo.getBlackId(), userId).size();
			
			vo.setThumbNum(thumbNum);
			vo.setCollectNum(collectNum);
			vo.setForwardNum(forwardNum);
			vo.setTopicNum(topicNum);
			
			vo.setThumbSelfNum(thumbSelfNum);
			vo.setCollectSelfNum(collectSelfNum);
			vo.setForwardSelfNum(forwardSelfNum);
		}
	}
	
	@SuppressWarnings("unchecked")
	private void transfer(Object result, Long userId) throws WpsException {
		PageVo<ListBlackVo> pageVo = (PageVo<ListBlackVo>) result;
		for (ListBlackVo vo : pageVo.getItems()) {
			DownloadBo downloadBo = new DownloadBo();
			downloadBo.setKeyList(vo.getPic());
			DownloadVo downloadVo = downloadProcessor.process(downloadBo);
			vo.setUrlList(downloadVo.getUrlList());
			
			Integer thumbNum = blackAppendRead.listByType(AppendTypeEnum.点赞.key(), vo.getBlackId(), null).size();
			Integer collectNum = blackAppendRead.listByType(AppendTypeEnum.收藏.key(), vo.getBlackId(), null).size();
			Integer forwardNum = blackAppendRead.listByType(AppendTypeEnum.转发.key(), vo.getBlackId(), null).size();
			Integer topicNum = blackTopicRead.listTopic(vo.getBlackId(), 0l).size();
			
			Integer thumbSelfNum = blackAppendRead.listByType(AppendTypeEnum.点赞.key(), vo.getBlackId(), userId).size();
			Integer collectSelfNum = blackAppendRead.listByType(AppendTypeEnum.收藏.key(), vo.getBlackId(), userId).size();
			Integer forwardSelfNum = blackAppendRead.listByType(AppendTypeEnum.转发.key(), vo.getBlackId(), userId).size();
			
			vo.setThumbNum(thumbNum);
			vo.setCollectNum(collectNum);
			vo.setForwardNum(forwardNum);
			vo.setTopicNum(topicNum);
			
			vo.setThumbSelfNum(thumbSelfNum);
			vo.setCollectSelfNum(collectSelfNum);
			vo.setForwardSelfNum(forwardSelfNum);
			
		}
	}
	
	
}

